using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Minions.Classes;
using Nsb.Classes;
using jQueryApi;

namespace MinionsEdit.Classes
{
    [IgnoreNamespace]
    public static class MiKeyMap
    {
        private static Dictionary<string, KeyAction> _editKeys;

        public static void InstallEvents()
        {
            if (_editKeys == null)
                _editKeys = new Dictionary(
                    "B",
                    (KeyAction) delegate(jQueryEvent e) { OnFocusDoEvo(e, MinionEdit.ActiveFocus.ToBack); },
                    "F",
                    (KeyAction) delegate(jQueryEvent e) { OnFocusDoEvo(e, MinionEdit.ActiveFocus.ToFront); },
                    "Ec",
                    (KeyAction) delegate(jQueryEvent e) { OnFocusDoEvo(e, MiMapActions.EditMinionProps); },
                    "8:",
                    (KeyAction) MinionEdit.ActiveDeleteMinionEv,
                    "8:s",
                    (KeyAction) MinionEdit.ActiveDeleteMinionEv,
                    "46:",
                    (KeyAction)MinionEdit.ActiveDeleteMinionEv,
                    "46:s",
                    (KeyAction)MinionEdit.ActiveDeleteMinionEv,
                    "Cc",
                    (KeyAction) MinionEdit.ActiveCopyEv,
                    "Vc",
                    (KeyAction) MinionEdit.ActivePasteEv,
                    "Rc",
                    (KeyAction) MinionEdit.ActiveRepaintEv,
                    "27:",
                    (KeyAction) MiUiOp.AbortEv
                    //"Zc",
                    //(KeyAction) MinionEdit.ActiveFocus.UndoEv
            );

            jQuery.Document.Bind("keydown.Minion", delegate(jQueryEvent e) { Keys.DoKeys(e, _editKeys); });
            jQuery.Select(Minion.DotMinionClassName).Live("mouseover.Minion", MinionEdit.MouseOverEv);
            jQuery.Select(Minion.DotMinionClassName).Live("mouseleave.Minion", MinionEdit.MouseOutEv);
            jQuery.Select(Minion.DotMinionClassName).Live("contextmenu.Minion", MiPropPanel.Ev);
            jQuery.Document.Bind("contextmenu", delegate(jQueryEvent e) { Evo.Trigger(MinionEdit.PageManagerEv, e); });
        }

        public static void RemoveEvents()
        {
            jQuery.Document.Unbind(".Minion");
            jQuery.Select(Minion.DotMinionClassName).Die(".Minion");
        }

        public static bool BailNoFocusEv(jQueryEvent e)
        {
            if (!HasFocus)
            {
                DomElement.CancelEvent(e);
                Cluster.FlashWindow();
                return true;
            }
            return false;
        }

        public static bool HasFocus
        {
            get { return MinionEdit.ActiveFocus != null; }
        }

        public static void OnFocusDoEvo(jQueryEvent e, Action<Evo> fn)
        {
            DomElement.CancelEvent(e);
            if (MinionEdit.ActiveFocus != null)
            {
                Inform.Debug("OnFocusDoEvo id={0}", MinionEdit.ActiveFocus.Id);
                new Evo().Response(MinionEdit.ActiveFocus.Save).SetValue(MinionEdit.ActiveFocus).Ev(e).FireWith(fn);
            }
            else
            {
                Cluster.FlashWindow();
            }
        }

    }
}